<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link rel="stylesheet" href="resource/js/layuimini-v2/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="resource/js/layuimini-v2/css/layuimini.css?v=2.0.4.2" media="all">
</head>
<body>
<table class="layui-hide" id="ID-table-demo-data" lay-filter="currentTableFilter"></table>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="resource/js/layuimini-v2/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="resource/js/layuimini-v2/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">
            新增超级表
        </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm data-search-btn" lay-event="search">
            查询SQL
        </button>
        <button class="layui-btn layui-btn-normal layui-btn-sm data-exec-btn" lay-event="search">
            执行SQL
        </button>
    </div>
</script>
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">打开</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="ddl">DDL</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
</script>
<script>
    layui.use(['jquery','table','miniTab'], function(){
        var $ = layui.jquery,
            table = layui.table,
            layer = layui.layer,
            miniTab = layui.miniTab;
        //miniTab.listen();

        var linkId = '{{.LinkId}}'
        var dbName = '{{.DbName}}'
        var data = [];
        {{range $index, $elem := .table}}
            var menuInfo ={
                "tableName": '{{$elem}}',
            }
            data.push(menuInfo)
        {{end}}

        // 已知数据渲染
        var inst = table.render({
            elem: '#ID-table-demo-data',
            toolbar: '#toolbarDemo',
            cols: [[ //标题栏
                {field: 'tableName', title: '表名', width: 200,},
                {title: '操作', width: 200, toolbar: '#currentTableBar', align: "center"}
            ]],
            data: data,
            //skin: 'line', // 表格风格
            //even: true,
            page: false, // 是否显示分页
            limits: [5, 10, 15],
            limit: 5 // 每页默认显示的数量
        });


        /**
         * toolbar监听事件
         */
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var tr = obj.tr; // 获取当前行的DOM对象
            if (obj.event === 'edit') {
                miniTab.openTdNewTabByIframe({
                    href:"list?linkId="+linkId+"&dbName="+dbName+"&tableName="+data.tableName,
                    title:dbName+"/"+data.tableName+"/表单数据",
                });

            } else if (obj.event === 'delete') {
                layer.confirm('确认删除'+data.tableName+"么?", function (index) {
                    var delData = {
                        linkId:linkId,
                        dbName:dbName,
                        tableName:data.tableName
                    }
                    $.ajax({
                        url:"/td/table/del/post",
                        type:"POST",
                        data:delData,
                        dataType : 'json',
                        async:false,
                        success:function (res) {
                            console.info(res)
                            if(res.code == 0){
                                obj.del();
                            }
                        }
                    });

                    layer.close(index);
                });
            } else if(obj.event === 'ddl'){
                var ddlData = {
                    linkId:linkId,
                    dbName:dbName,
                    TableName:data.tableName
                }
                $.ajax({
                    url:"/td/table/ddl/post",
                    type:"POST",
                    data:ddlData,
                    dataType : 'json',
                    async:false,
                    success:function (res) {
                        console.info(res)
                        if(res.code == 0){
                            var ddl = res.data.result;
                            var ddlHtml = '<div>'+ddl+'</div>';
                            layer.open({
                                type: 1,
                                title: 'DDL',
                                content: ddlHtml
                            })
                        }

                    }
                });
            }
        });

        $(".data-search-btn").on("click",function (){
            miniTab.openTdNewTabByIframe({
                href:"query?linkId="+linkId+"&dbName="+dbName,
                title:dbName+"/查询SQL",
            });
        });

        $(".data-exec-btn").on("click",function (){
            miniTab.openTdNewTabByIframe({
                href:"execsql?linkId="+linkId+"&dbName="+dbName,
                title:dbName+"/执行SQL",
            });
        });

        $(".data-add-btn").on("click",function (){
            miniTab.openTdNewTabByIframe({
                href:"createtable?linkId="+linkId+"&dbName="+dbName,
                title:dbName+"/新增表",
            });
        });
    });
</script>

</body>
</html>